package cn.sxt.yxl.question.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.dao.IAColomnsDao;
import cn.sxt.yxl.question.dao.IAItemsDao;
import cn.sxt.yxl.question.pojo.AColomns;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.service.IQuestionService;

import cn.sxt.yxl.user.dao.IARoleDao;

@Service("questionService")
public class QuestionServiceImpl implements IQuestionService{
	
	
	
	@Resource
	private IAColomnsDao columnsDao;
	@Resource
	private IAItemsDao itemsDao;
	
	
	
	@Override
	public AColomns findOneColomnsService(Map<String, Object> condMap) {		
		return this.columnsDao.findOne(condMap);				
	}


	@Override
	public JSONObject saveOneColomnsService(AColomns columns) {
		JSONObject resultJSON = new JSONObject();
		int res=this.columnsDao.saveOne(columns);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", columns.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}


	@Override
	public JSONObject updateOneColomnsService(AColomns columns) {
		JSONObject resultJSON = new JSONObject();
		int res=this.columnsDao.updateOne(columns);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", columns.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}


	@Override
	public JSONObject deleteOneColomnsService(AColomns columns) {
		JSONObject resultJSON = new JSONObject();
		int res=this.columnsDao.deleteOne(columns);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", columns.getId());
			dataJson.put("effectCount", res);
			
			resultJSON.put("data",dataJson);
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}


	@Override
	public List<AColomns> findCondListColomnsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil !=null){
			//获取分页信息
			Page page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<AColomns> columnssList=this.columnsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return columnssList;
		}
		return this.columnsDao.findCondList(condMap);
	}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------


	@Override
	public JSONObject saveOneItemsService(AItems items) {
		JSONObject resultJSON = new JSONObject();
		int res = this.itemsDao.saveOne(items);
		if(res > 0 )
		{
			/* 成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", items.getId() + "");
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");
		}else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}


	@Override
	public JSONObject deleteOneItemsService(AItems items) {
		JSONObject resultJSON = new JSONObject();
		int res = this.itemsDao.deleteOne(items);
		if(res > 0 )
		{
			/* 成功 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", items.getId() + "");
			/* 影响条数 */
			dataJSON.put("effect", res + "");
			resultJSON.put("data", dataJSON);
			
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		}else
		{
			/* 失败 */
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}


	@Override
	public AItems findOneItemsService(Map<String, Object> condMap) {
		return this.itemsDao.findOne(condMap);
	}


	@Override
	public List<AItems> findItemsCondListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") +  "%");
		}
		if (pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			Page<AItems> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询数据 */
			List<AItems> columnsList = this.itemsDao.findCondList(condMap);
			/* 设置总记录数 ,一定要在查询结果之后 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return columnsList;
		}
		return this.itemsDao.findCondList(condMap);
	}
	
	
}
